package com.example.hfweather.ui.me

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.hfweather.R
import com.example.hfweather.WeatherApplication
import com.example.hfweather.base.BaseBindingFragment
import com.example.hfweather.databinding.FragmentMeBinding
import com.example.hfweather.storage.TokenPreferences
import com.example.hfweather.ui.login.LoginActivity
import com.example.hfweather.util.RetrofitClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MeFragment : BaseBindingFragment<FragmentMeBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getUserInfo()


    }

    override fun onResume() {
        super.onResume()
        getUserInfo()
    }

    override fun initBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): FragmentMeBinding {
        return FragmentMeBinding.inflate(inflater, parent, false)
    }

    override fun initView() {
        //点击跳转修改头像
        binding.ivAvatar.setOnClickListener {gotoChangeAvatar()}
        binding.ivEditAvatar.setOnClickListener {gotoChangeAvatar()}
        //退出登录
        binding.btnLogout.setOnClickListener {
            //清除 Token
            TokenPreferences.getInstance(requireContext()).removeToken()
            //跳转登录页面
            startActivity(Intent(requireContext(), LoginActivity::class.java))
        }
    }

    /**
     * 获取用户信息
     */
    private fun getUserInfo() {
        lifecycleScope.launch(Dispatchers.Main) {
            //切换到Io线程发送网络请求‘
            val res = withContext(Dispatchers.IO) {
                RetrofitClient.apiService.getUserInfoApi()
            }
            if (res.code == 10000) {
                Toast.makeText(requireContext(), "获取用户信息成功", Toast.LENGTH_SHORT).show()
                //保存用户信息到全局中
                if (res.data != null){
                    WeatherApplication.getInstance()?.initUser(res.data)
                }

                //头像
                Glide.with(this@MeFragment)
                    .load(res.data?.avatar)
                    .apply(RequestOptions ()
                        .placeholder(R.mipmap.avatar1)
                        .error(R.mipmap.avatar1))
                    .into(binding.ivAvatar)
                //签名
                binding.tvSignature.text = res.data?.motto
                //昵称
                binding.tvNickname.text = res.data?.nick_name
                //性别
                binding.tvGender.text =res.data?.gender
                //用户名
                binding.tvAccount.text = res.data?.username



            } else {
                Toast.makeText(requireContext(), res.message, Toast.LENGTH_SHORT).show()
            }
        }
    }

    /**
     * 跳转修改头像
     */
    private fun gotoChangeAvatar() {
        val intent = Intent(requireActivity(), ChangeAvatarActivity::class.java)
        startActivity(intent)
    }


}